package edu.cebanc.spring.biblioteca.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

import edu.cebanc.spring.biblioteca.bean.AuthorBean;
import edu.cebanc.spring.biblioteca.bean.BookBean;
import edu.cebanc.spring.biblioteca.bean.BookCopyBean;
import edu.cebanc.spring.biblioteca.bean.GenreBean;
import edu.cebanc.spring.biblioteca.bean.LanguageBean;
import edu.cebanc.spring.biblioteca.bean.PublisherBean;
import edu.cebanc.spring.biblioteca.bean.UserBean;
import edu.cebanc.spring.biblioteca.domain.Author;
import edu.cebanc.spring.biblioteca.domain.Book;
import edu.cebanc.spring.biblioteca.domain.BookCopy;
import edu.cebanc.spring.biblioteca.domain.Genre;
import edu.cebanc.spring.biblioteca.domain.Language;
import edu.cebanc.spring.biblioteca.domain.Publisher;
import edu.cebanc.spring.biblioteca.domain.User;

public class DataParser {
	
	public User UserBean2User(UserBean ub) {
		User user = new User();
		
		user.setId_usuario(ub.getId_usuario());
		user.setEmail(ub.getEmail());
		user.setNombre(ub.getNombre());
		user.setApellidos(ub.getApellidos());
		user.setNif(ub.getNif());
		user.setCalle(ub.getCalle());
		user.setInstrucciones(ub.getInstrucciones());
		user.setCiudad(ub.getCiudad());
		user.setCp(ub.getCp());
		user.setProvincia(ub.getProvincia());
		user.setCcaa(ub.getCcaa());
		user.setTelefono(ub.getTelefono());
		user.setMovil(ub.getMovil());
		
		return user;
	}
	
	public UserBean User2UserBean (User user) {
		UserBean ub = new UserBean();
		
		ub.setId_usuario(user.getId_usuario());
		ub.setEmail(user.getEmail());
		ub.setNombre(user.getNombre());
		ub.setApellidos(user.getApellidos());
		ub.setNif(user.getNif());
		ub.setCalle(user.getCalle());
		ub.setInstrucciones(user.getInstrucciones());
		ub.setCiudad(user.getCiudad());
		ub.setCp(user.getCp());
		ub.setProvincia(user.getProvincia());
		ub.setCcaa(user.getCcaa());
		ub.setTelefono(user.getTelefono());
		ub.setMovil(user.getMovil());
		
		return ub;		
	}
	
	public List<UserBean> UserList2UserBeanList (List<User> userList) {
		List<UserBean> ubl = new ArrayList<UserBean>();
		
		for(int i=0; i < userList.size(); i++) {
			ubl.add(this.User2UserBean(userList.get(i)));
		}
		
		return ubl;
	}
	
	public Book BookBean2Book(BookBean bb) {
		Book book = new Book();
		
		book.setId_libro(bb.getId_libro());
		book.setIsbn(bb.getIsbn());
		book.setFecha(bb.getFecha());
	    //book.setAutores(bb.getAutores());
	    //book.setGeneros(bb.getGeneros());
		
		return book;
	}
	
	@SuppressWarnings("rawtypes")
	public BookBean Book2BookBean (Book book) {
		BookBean bb = new BookBean();
		
		bb.setId_libro(book.getId_libro());
		bb.setIsbn(book.getIsbn());
		bb.setFecha(book.getFecha());
		
		Iterator it = book.getAutores().iterator();
		
		while(it.hasNext()) {
			bb.getAutores().add(this.Author2AuthorBean((Author)it.next()));
		}
		
		it = book.getGeneros().iterator();
		
		while(it.hasNext()) {
			bb.getGeneros().add(this.Genre2GenreBean((Genre)it.next()));
		}
		
		return bb;		
	}
	
	public List<BookBean> BookList2BookBeanList (List<Book> bookList) {
		List<BookBean> ubl = new ArrayList<BookBean>();
		
		for(int i=0; i < bookList.size(); i++) {
			ubl.add(this.Book2BookBean(bookList.get(i)));
		}
		
		return ubl;
	}
	
	public Author AuthorBean2Author (AuthorBean ab) {
		Author author = new Author();
		
		author.setId_autor(ab.getId_autor());
		author.setNombre(ab.getNombre());
		author.setApellidos(ab.getApellidos());
		
		return author;
	}
	
	public AuthorBean Author2AuthorBean (Author author) {
		AuthorBean ab = new AuthorBean();
		
		ab.setId_autor(author.getId_autor());
		ab.setNombre(author.getNombre());
		ab.setApellidos(author.getApellidos());
		
		return ab;
	}
	
	public List<AuthorBean> AuthorList2AuthorBeanList (List<Author> authorList) {
		List<AuthorBean> abl = new ArrayList<AuthorBean>();
		
		for(int i=0; i < authorList.size(); i++) {
			abl.add(this.Author2AuthorBean(authorList.get(i)));
		}
		
		return abl;
	}
	
	public Language LanguageBean2Language (LanguageBean lb) {
		Language lang = new Language();
		
		lang.setId_idioma(lb.getId_lang());
		lang.setNombre(lb.getNombre());
				
		return lang;
	}
	
	public LanguageBean Language2LanguageBean (Language lang) {
		LanguageBean lb = new LanguageBean();
		
		lb.setId_lang(lang.getId_idioma());
		lb.setNombre(lang.getNombre());
		
		return lb;
	}
	
	public List<LanguageBean> LanguageList2LanguageBeanList (List<Language> langList) {
		List<LanguageBean> lbl = new ArrayList<LanguageBean>();
		
		for(int i=0; i < langList.size(); i++) {
			lbl.add(this.Language2LanguageBean(langList.get(i)));
		}
		
		return lbl;
	}
	
	public Publisher PublisherBean2Publisher (PublisherBean pb) {
		Publisher pub = new Publisher();
		
		pub.setId_editorial(pb.getId_pub());
		pub.setNombre(pb.getNombre());
		
		return pub;
	}
	
	public PublisherBean Publisher2PublisherBean (Publisher pub) {
		PublisherBean pb = new PublisherBean();
		
		pb.setId_pub(pub.getId_editorial());
		pb.setNombre(pub.getNombre());
		
		return pb;
	}
	
	public List<PublisherBean> PublisherList2PublisherBeanList (List<Publisher> authorList) {
		List<PublisherBean> abl = new ArrayList<PublisherBean>();
		
		for(int i=0; i < authorList.size(); i++) {
			abl.add(this.Publisher2PublisherBean(authorList.get(i)));
		}
		
		return abl;
	}
	
	public Genre GenreBean2Genre (GenreBean gb) {
		Genre genre = new Genre();
		
		genre.setId_genero(gb.getId_genre());
		genre.setNombre(gb.getNombre());
				
		return genre;
	}
	
	public GenreBean Genre2GenreBean (Genre genre) {
		GenreBean gb = new GenreBean();
		
		gb.setId_genre(genre.getId_genero());
		gb.setNombre(genre.getNombre());
		
		return gb;
	}
	
	public List<GenreBean> GenreList2GenreBeanList (List<Genre> genreList) {
		List<GenreBean> abl = new ArrayList<GenreBean>();
		
		for(int i=0; i < genreList.size(); i++) {
			abl.add(this.Genre2GenreBean(genreList.get(i)));
		}
		
		return abl;
	}
	
	public BookCopy BookCopyBean2BookCopy (BookCopyBean bcb) {
		BookCopy bookCopy = new BookCopy();
		
		bookCopy.setId_ejemplar(bcb.getId_ejemplar());
		bookCopy.setTitulo(bcb.getTitulo());
		bookCopy.setEstado(bcb.getEstado());
		bookCopy.setFecha_alta(bcb.getFecha_alta());
		bookCopy.setId_idioma(this.LanguageBean2Language(bcb.getId_idioma()));
		bookCopy.setId_editorial(this.PublisherBean2Publisher(bcb.getId_editorial()));
		
		return bookCopy;
	}
	
	public BookCopyBean BookCopy2BookCopyBean (BookCopy bookCopy) {
		BookCopyBean bcb = new BookCopyBean();
		
		bcb.setId_ejemplar(bookCopy.getId_ejemplar());
		bcb.setTitulo(bookCopy.getTitulo());
		bcb.setEstado(bookCopy.getEstado());
		bcb.setFecha_alta(bookCopy.getFecha_alta());
		bcb.setId_idioma(this.Language2LanguageBean(bookCopy.getId_idioma()));
		bcb.setId_editorial(this.Publisher2PublisherBean(bookCopy.getId_editorial()));
		
		return bcb;
	}
	
	public List<BookCopyBean> BookCopyList2BookCopyBeanList (List<BookCopy> bookCopyList) {
		List<BookCopyBean> bcbl = new ArrayList<BookCopyBean>();
		
		for(int i=0; i < bookCopyList.size(); i++) {
			bcbl.add(this.BookCopy2BookCopyBean(bookCopyList.get(i)));
		}
		
		return bcbl;
	}

}
